<!--
    JBoss, Home of Professional Open Source
    Copyright 2015, Red Hat, Inc. and/or its affiliates, and individual
    contributors by the @authors tag. See the copyright.txt in the
    distribution for a full listing of individual contributors.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
>
<h:head>
    <title>Batch Processing</title>
    <link rel="stylesheet" href="css/patternfly.css"/>
    <link rel="stylesheet" href="css/font-awesome.css"/>
    <script src="js/jquery-2.0.3.js"></script>
    <script type="text/javascript" src="js/bootstrap.js"/>
</h:head>
<body>
    <h:form id="form" styleClass="form-horizontal">
        <div class="container">
            <div class="row">
                <div class="col-md-4">
                    <h2>Generate a import file</h2>
                    This form will generate a file with the number of records specified.
                    <div class="form-group">
                        <h:outputLabel value="File name:" styleClass="col-sm-5 control-label" />
                        <div class="col-sm-5">
                            <h:inputText id="fileName" value="#{batchController.fileName}" />
                            <h:message for="fileName" styleClass="help-block" />
                         </div>
                    </div>
                    <div class="form-group">
                        <h:outputLabel value="Number of records:" styleClass="col-sm-5 control-label" />
                        <div class="col-sm-5">
                            <h:inputText id="numRecords" value="#{batchController.numRecords}" >
                                <f:convertNumber integerOnly="true" />
                            </h:inputText>
                            <h:message for="numRecords" styleClass="help-block" />
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-7">
                            <h:selectBooleanCheckbox value="#{batchController.generateWithError}" />
                            Generate a duplicate record (forced error)
                        </div>
                    </div>
                    <div class="btn-group-vertical">
                            <h:commandButton styleClass="btn btn-default btn-sm" action="#{batchController.generateFileAndStarJob()}" value="Generate a new file and start import job" >
                                <f:ajax execute="@form" render="@form"/>
                            </h:commandButton>
                            <h:commandButton styleClass="btn btn-default btn-sm" action="#{batchController.generate()}" value="Generate new file" >
                                <f:ajax execute="@form" render="@form"/>
                            </h:commandButton>
                            <h:commandButton styleClass="btn btn-default btn-sm" action="#{batchController.startImport()}" value="Start import Job" >
                                <f:ajax execute="@form"  render="@form"/>
                            </h:commandButton>
                    </div>
                    <div style="padding-top: 3em;">
                        <h:messages globalOnly="true" errorStyle="color: red;" warnStyle="color: yellow;"/>
                    </div>
                </div>
                <div class="col-md-8">
                    <h2>Instructions</h2>
                    <h3>Usage 1: Import the file without any errors</h3>
                     Click on <strong>Generate a new file and start import job</strong> button. This will generate a new file with 10 unique records to be imported. After the file is generated, the import job will start. You will see a table containing information about the task that was just started. You can click on <strong>Update jobs list</strong> button and verify that the job was completed.
                     
                     <h3>Usage 2: Import an error file and fix it</h3>
                     Check the <strong>Generate a duplicate record</strong> checkbox and click on <strong>Generate a new file and start import job</strong> button. If you click on <strong>Update jobs list</strong> button, you will see that the job failed with the following Exit Status: <em>Error : org.hibernate.exception.ConstraintViolationException: could not execute statement</em>. This was caused because the job tried to insert a duplicate record at the Database. You will also see `org.h2.jdbc.JdbcSQLException: Unique index or primary key violation` exception stacktraces in the server log.
                     Uncheck the <strong>Generate a duplicate record</strong> checkbox and click on <strong>Generate a new file</strong> button. This will generate file without errors.
                     Click on <strong>Restart</strong> button in the last column for that job instance in the <strong>List of Jobs</strong> table. If you  click on <strong>Update jobs list</strong> button to verify that the job was completed. Analyze the logs and check that the job started from the last checkpoint.
                    
                     <h3>Usage 3: Import an error file and do not fix the errors </h3>
                     Check the <strong>Generate a duplicate record</strong> checkbox and click on <strong>Generate a new file and start import job</strong> button. If you click on <strong>Update jobs list</strong> button, you will see that the job failed with the following Exit Status: <em>Error : org.hibernate.exception.ConstraintViolationException: could not execute statement</em>. This was caused because the job tried to insert a duplicate record at the Database.
                     Just click on <strong>Restart</strong> button again. If you  click on <strong>Update jobs list</strong> button, you will see that the job was marked as <strong>ABANDONED</strong> this time because it was restarted once. Notice that there's a new parameter: `restartedOnce=true`.
                </div>
            </div>
            <hr />
            <div class="row">
                    <h2>List of Jobs</h2>
                    <br />
                    <h:commandButton id="refreshButton" styleClass="btn btn-default"  value="Update jobs list" >
                        <f:ajax execute="@this" render="@form"/>
                    </h:commandButton>
                    <h:dataTable id="dataTable" value="#{batchController.jobsExecution}" var="job" width="100%" style="text-align: center;" border="1"
                        rendered="#{batchController.jobsExecution.size() > 0}"
                        styleClass="table table-striped table-bordered table-hover">
                        <h:column>
                            <f:facet name="header">Instance Id</f:facet>
                            #{job.jobInstanceId}
                        </h:column>
                        <h:column>
                            <f:facet name="header">Execution Id</f:facet>
                            #{job.executionId}
                        </h:column>
                        <h:column>
                            <f:facet name="header">Parameters</f:facet>
                            #{job.jobParameters}
                        </h:column>
                        <h:column>
                            <f:facet name="header">Start Time</f:facet>
                            <h:outputText value="#{job.startTime}">
                                <f:convertDateTime type="time" timeZone="#{timeZone}" />
                            </h:outputText>
                        </h:column>
                        <h:column>
                            <f:facet name="header">End Time</f:facet>
                            <h:outputText value="#{job.endTime}">
                                <f:convertDateTime type="time" timeZone="#{timeZone}" />
                            </h:outputText>
                        </h:column>
                        <h:column>
                            <f:facet name="header">Batch status</f:facet>
                            #{job.batchStatus}
                        </h:column>
                        <h:column>
                            <f:facet name="header">Exit status</f:facet>
                            #{job.exitStatus}
                        </h:column>
                        <h:column>
                            <f:facet name="header">Operation</f:facet>
                            <h:commandButton value="Restart" rendered="#{job.batchStatus == 'FAILED'}"
                                action="#{batchController.restartJob(job.executionId)}">
                                <f:ajax render="@form"/>
                            </h:commandButton>
                        </h:column>
                </h:dataTable>
            </div>
        </div>
    </h:form>
</body>
</html>
